package com.ruoyi.kjcg.sbgf.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kjcg.sbgf.domain.Sbgf;
import com.ruoyi.kjcg.sbgf.domain.SbgfFj;
import com.ruoyi.kjcg.sbgf.service.ISbgfService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 省部工法Controller
 *
 * @author huacai
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/qygf/sbgf")
public class SbgfController extends BaseController {
    @Resource
    private ISbgfService sbgfService;

    /**
     * 查询省部工法列表
     */
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sbgf sbgf) {
        startPage();
        List<Sbgf> list = sbgfService.selectSbgfList(sbgf);
        return getDataTable(list);
    }

    /**
     * 导出省部工法列表
     */
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:export')")
    @Log(title = "省部工法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sbgf sbgf) {
        List<Sbgf> list = sbgfService.selectSbgfList(sbgf);
        ExcelUtil<Sbgf> util = new ExcelUtil<Sbgf>(Sbgf.class);
        util.exportExcel(response, list, "省部工法数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Sbgf> util = new ExcelUtil<Sbgf>(Sbgf.class);
        util.importTemplateExcel(response, "省部工法数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "省部工法", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Sbgf> util = new ExcelUtil<Sbgf>(Sbgf.class);
        InputStream inputStream = file.getInputStream();
        List<Sbgf> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sbgfService.batchInsertSbgf(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取省部工法详细信息
     */
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:query')")
    @GetMapping(value = "/{sbgfId}")
    public AjaxResult getInfo(@PathVariable("sbgfId") String sbgfId) {
        return success(sbgfService.selectSbgfBySbgfId(sbgfId));
    }

    /**
     * 新增省部工法
     */
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:add')")
    @Log(title = "省部工法", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sbgf sbgf) {
        sbgf.setUserId(Math.toIntExact(getUserId())); //用户ID
        sbgf.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        sbgf.setCreateBy(getUsername()); //创建人
        sbgf.setIdWhere("存在");
        return toAjax(sbgfService.insertSbgf(sbgf));
    }

    /**
     * 修改省部工法
     */
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:edit')")
    @Log(title = "省部工法", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sbgf sbgf) {
        sbgf.setUpdateBy(getUsername()); //修改人
        return toAjax(sbgfService.updateSbgf(sbgf));
    }

    /**
     * 删除省部工法
     */
    @PreAuthorize("@ss.hasPermi('qygf:sbgf:remove')")
    @Log(title = "省部工法", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sbgfIds}")
    public AjaxResult remove(@PathVariable String[] sbgfIds) {
        return toAjax(sbgfService.deleteSbgfBySbgfIds(sbgfIds));
    }

    /**
     * 获得省部工法主键ID (UUID)
     */
    @GetMapping("/getSbGfId")
    public AjaxResult getSbGfId(){
        String getSbGfId = IdUtils.fastSimpleUUID();
        return success(getSbGfId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertSbgfFjBySbgfId(@RequestBody SbgfFj sbgfFj){
        return toAjax(sbgfService.insertSbgfFjBySbgfId(sbgfFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectSbgfFjList(String sId){
        SbgfFj sbgfFj = new SbgfFj();
        sbgfFj.setSbgfId(sId);
        return AjaxResult.success(sbgfService.selectSbgfFjList(sbgfFj));
    }

    /**
     * 删除省部工法附件
     */
    @Log(title = "省部工法附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteSbgfFjByFjIds(@PathVariable Integer[] fjIds)
    {
        return toAjax(sbgfService.deleteSbgfFjByFjIds(fjIds));
    }

}
